package com.android.clinic.app.ui.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.clinic.app.R;
import com.android.clinic.app.base.BaseActivity;
import com.android.clinic.app.ui.search.FlowTagLayout;
import com.android.clinic.app.ui.search.OnInitSelectedPosition;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/2/5.
 * 生活习惯
 */
public class LivingHabitActivity extends BaseActivity {

    public static final int LIVING_CODE = 999;

    private FlowTagLayout mHotFlowLay;
    private String tagStr = "";

    private List<String> selectList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_living_habit;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        setTitleBarTitle("生活习惯");
        setTitleBarLeftResource(R.drawable.icon_back);
        setTitleBarRightText("保存");
        mHotFlowLay = (FlowTagLayout) findViewById(R.id.hot_flow_layout);

        tagStr = this.getIntent().getStringExtra("livingHabit");
        if (!TextUtils.isEmpty(tagStr)) {
            String[] tagArray = tagStr.split(",");
            int tagSize = tagArray.length;
            for (int i = 0; i < tagSize; i++) {
                selectList.add(tagArray[i]);
            }
            tagStr = "," + tagStr;
        }
        initHotTags();
        mHotFlowLay.setOnTagClickListener((parent, view, position) -> {
            TagAdapter.TagHolder holder = (TagAdapter.TagHolder) view.getTag();
            holder.tvTag.setSelected(!holder.tvTag.isSelected());
            if (holder.tvTag.isSelected()) {
                tagStr +=  "," + holder.tvTag.getText().toString();
            } else {
                tagStr = tagStr.replace("," + holder.tvTag.getText().toString(), "");
            }
        });
    }

    @Override
    protected void onRightClickListener(View v) {
        if (TextUtils.isEmpty(tagStr)) {
            tagStr = "";
        } else {
            tagStr = tagStr.substring(1, tagStr.length());
        }
        Intent intent = new Intent();
        intent.putExtra("livingHabit", tagStr);
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 初始化热门搜索标签
     */
    private void initHotTags() {
        List<String> dataList = new ArrayList<>();
        dataList.add("暴饮暴食");
        dataList.add("嗜食生冷");
        dataList.add("嗜食辛辣");
        dataList.add("嗜食油腻");
        dataList.add("常饮酒");
        dataList.add("常抽烟");
        dataList.add("晚上11点后睡觉");
        dataList.add("用眼过度");
        dataList.add("爱生闷气");
        dataList.add("经常发火");
        dataList.add("思虑过度");
        dataList.add("过于劳累");
        dataList.add("久坐");
        dataList.add("久站");
        dataList.add("过多行走");
        dataList.add("悲伤难止");
        dataList.add("经常害怕");
        TagAdapter tagAdapter = new TagAdapter();
        mHotFlowLay.setAdapter(tagAdapter);
        tagAdapter.addHotKeyWord(dataList);
    }

    private class TagAdapter extends BaseAdapter implements OnInitSelectedPosition {

        private List<String> dataList = new ArrayList<>();
        public int position;

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TagHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(LivingHabitActivity.this)
                        .inflate(R.layout.item_search_tag_layout, null);
                holder = new TagHolder();
                holder.tvTag = (TextView) convertView.findViewById(R.id.tv_tag);
                convertView.setTag(holder);
            } else {
                holder = (TagHolder) convertView.getTag();
            }

            holder.tvTag.setSelected(false);
            if (null != selectList && selectList.size() > 0) {
                int selectSize = selectList.size();
                for (int i = 0; i < selectSize; i++){
                    if (selectList.get(i).equals(dataList.get(position))) {
                        holder.tvTag.setSelected(true);
                    }
                }
            }
            holder.tvTag.setText(dataList.get(position));

            return convertView;
        }

        @Override
        public boolean isSelectedPosition(int poi) {
            return poi == position;
        }

        private void addHotKeyWord(List<String> data) {
            dataList.addAll(data);
            notifyDataSetChanged();
        }

        class TagHolder {
            TextView tvTag;
        }

    }
}
